<?php

class Control_PortfolioController extends Zend_Controller_Action
{

    public function init()
    {
        $this->_helper->layout->setLayout('admin');
        $this->yesButton='Yes'; //напис на кнопці Так під час видалення
        $this->noButton='No'; //напис на кнопці Ні під час видалення
        $this->portfolioDBb = new Control_Model_Table_Portfolio();
        $this->view->controller=$this->getRequest()->getControllerName();
        $this->view->action=$this->getRequest()->getActionName();
    }

    public function indexAction()
    {
        $this->_helper->redirector('edit');
    }

    public function addAction()
    {
        $form = new Control_Model_Form_Portfolio();
        $form->submit->setLabel('Add');
        $this->view->form= $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {

                $adapter = $form->getElement('logo')->getTransferAdapter();
                $file = $adapter->getFileInfo();
                    $ext = split("[/\\.]", $file['logo']['name']);
                    $valueForReplace = array(" ",".");
                    $newName = str_replace($valueForReplace,"",microtime()).'.'.$ext[1];
                    $adapter->addFilter('Rename', realpath(dirname('.')).
                    DIRECTORY_SEPARATOR.
                    'images'.
                    DIRECTORY_SEPARATOR.
                    'user'.
                    DIRECTORY_SEPARATOR.
                    'original'.
                    DIRECTORY_SEPARATOR.
                    $newName);
                $adapter->receive($file['logo']['name']);
                $logo = '/'.$file['logo']['destination'].'/'.$newName;
//-----------------------------3IMAGES------------------------------------------
                $adapter1 = $form->getElement('img1')->getTransferAdapter();
                $file = $adapter1->getFileInfo();
                if ($file['img1']['name']!=""){
                    $ext = split("[/\\.]", $file['img1']['name']);
                    $valueForReplace = array(" ",".");
                    $newName = str_replace($valueForReplace,"",microtime()).'.'.$ext[1];
                    $adapter1->addFilter('Rename', realpath(dirname('.')).
                    DIRECTORY_SEPARATOR.
                    'images'.
                    DIRECTORY_SEPARATOR.
                    'user'.
                    DIRECTORY_SEPARATOR.
                    'original'.
                    DIRECTORY_SEPARATOR.
                    $newName);
                $adapter1->receive($file['img1']['name']);
                $img1 = '/'.$file['img1']['destination'].'/'.$newName;
                }else{$img1="";};
                //
                $adapter2 = $form->getElement('img2')->getTransferAdapter();
                $file = $adapter2->getFileInfo();
                if ($file['img2']['name']!=""){
                    $ext = split("[/\\.]", $file['img2']['name']);
                    $valueForReplace = array(" ",".");
                    $newName = str_replace($valueForReplace,"",microtime()).'.'.$ext[1];
                    $adapter2->addFilter('Rename', realpath(dirname('.')).
                    DIRECTORY_SEPARATOR.
                    'images'.
                    DIRECTORY_SEPARATOR.
                    'user'.
                    DIRECTORY_SEPARATOR.
                    'original'.
                    DIRECTORY_SEPARATOR.
                    $newName);
                $adapter2->receive($file['img2']['name']);
                $img2 = '/'.$file['img2']['destination'].'/'.$newName;
                }else{$img2="";};
                //
                $adapter3 = $form->getElement('img3')->getTransferAdapter();
                $file = $adapter3->getFileInfo();
                if ($file['img3']['name']!=""){
                    $ext = split("[/\\.]", $file['img3']['name']);
                    $valueForReplace = array(" ",".");
                    $newName = str_replace($valueForReplace,"",microtime()).'.'.$ext[1];
                    $adapter3->addFilter('Rename', realpath(dirname('.')).
                    DIRECTORY_SEPARATOR.
                    'images'.
                    DIRECTORY_SEPARATOR.
                    'user'.
                    DIRECTORY_SEPARATOR.
                    'original'.
                    DIRECTORY_SEPARATOR.
                    $newName);
                $adapter3->receive($file['img3']['name']);
                $img3 = '/'.$file['img3']['destination'].'/'.$newName;
                }else{$img3="";};
//-------------------------ENDOF3IMAGES----------------------------------------
//                Zend_Debug::dump($adapter->getFileInfo());exit();
                $logo_link = $form->getValue('logo_link');
                $name = $form->getValue('name');
                $link = $form->getValue('link');
                $langs = $form->getValue('langs');
                $about = $form->getValue('about');
                $class = $form->getValue('class');
                $this->portfolioDBb->addData($logo, $img1, $img2, $img3, $logo_link, $name, $link, $langs, $about, $class);
                $this->_helper->redirector('edit');
            } else {
                $form->populate($formData);
            }
        }

    }

    public function editAction()
    {

        $this->view->PortfolioData = $this->portfolioDBb->getAll();
        $form=new Control_Model_Form_Portfolio();
        $form->submit->setLabel('Update');
        $this->view->form = $form;
        $delparam=$this->_getParam('del', 0);

        if ($this->getRequest()->isPost() && ($delparam!=1)) { //Якщо відправлена форма без видалення
        $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
            $id = (int)$form->getValue('id');
                $adapter = $form->getElement('logo')->getTransferAdapter();
                $file = $adapter->getFileInfo();
                if ($file['logo']['name']!=""){
                    $ext = split("[/\\.]", $file['logo']['name']);
                    $valueForReplace = array(" ",".");
                    $newName = str_replace($valueForReplace,"",microtime()).'.'.$ext[1];
                    $adapter->addFilter('Rename', realpath(dirname('.')).
                    DIRECTORY_SEPARATOR.
                    'images'.
                    DIRECTORY_SEPARATOR.
                    'user'.
                    DIRECTORY_SEPARATOR.
                    'original'.
                    DIRECTORY_SEPARATOR.
                    $newName);
                $adapter->receive($file['logo']['name']);
                $logo = '/'.$file['logo']['destination'].'/'.$newName;
                }else{$logo="";};

//-----------------------------3IMAGES------------------------------------------
                $adapter1 = $form->getElement('img1')->getTransferAdapter();
                $file = $adapter1->getFileInfo();
                if ($file['img1']['name']!=""){
                    $ext = split("[/\\.]", $file['img1']['name']);
                    $valueForReplace = array(" ",".");
                    $newName = str_replace($valueForReplace,"",microtime()).'.'.$ext[1];
                    $adapter1->addFilter('Rename', realpath(dirname('.')).
                    DIRECTORY_SEPARATOR.
                    'images'.
                    DIRECTORY_SEPARATOR.
                    'user'.
                    DIRECTORY_SEPARATOR.
                    'original'.
                    DIRECTORY_SEPARATOR.
                    $newName);
                $adapter1->receive($file['img1']['name']);
                $img1 = '/'.$file['img1']['destination'].'/'.$newName;
                }else{$img1="";};
                //
                $adapter2 = $form->getElement('img2')->getTransferAdapter();
                $file = $adapter2->getFileInfo();
                if ($file['img2']['name']!=""){
                    $ext = split("[/\\.]", $file['img2']['name']);
                    $valueForReplace = array(" ",".");
                    $newName = str_replace($valueForReplace,"",microtime()).'.'.$ext[1];
                    $adapter2->addFilter('Rename', realpath(dirname('.')).
                    DIRECTORY_SEPARATOR.
                    'images'.
                    DIRECTORY_SEPARATOR.
                    'user'.
                    DIRECTORY_SEPARATOR.
                    'original'.
                    DIRECTORY_SEPARATOR.
                    $newName);
                $adapter2->receive($file['img2']['name']);
                $img2 = '/'.$file['img2']['destination'].'/'.$newName;
                }else{$img2="";};
                //
                $adapter3 = $form->getElement('img3')->getTransferAdapter();
                $file = $adapter3->getFileInfo();
                if ($file['img3']['name']!=""){
                    $ext = split("[/\\.]", $file['img3']['name']);
                    $valueForReplace = array(" ",".");
                    $newName = str_replace($valueForReplace,"",microtime()).'.'.$ext[1];
                    $adapter3->addFilter('Rename', realpath(dirname('.')).
                    DIRECTORY_SEPARATOR.
                    'images'.
                    DIRECTORY_SEPARATOR.
                    'user'.
                    DIRECTORY_SEPARATOR.
                    'original'.
                    DIRECTORY_SEPARATOR.
                    $newName);
                $adapter3->receive($file['img3']['name']);
                $img3 = '/'.$file['img3']['destination'].'/'.$newName;
                }else{$img3="";};
//-------------------------ENDOF3IMAGES----------------------------------------

//            Zend_Debug::dump($logo);exit();
            $logo_link = $form->getValue('logo_link');
            $name = $form->getValue('name');
            $link = $form->getValue('link');
            $langs = $form->getValue('langs');
            $about = $form->getValue('about');
            $class = $form->getValue('class');
//            Zend_Debug::dump($logo);echo '000';exit();
            $this->portfolioDBb->updateData($id, $logo, $img1, $img2, $img3, $logo_link, $name, $link, $langs, $about, $class);
            $this->_helper->redirector('edit');
        } else {
            $form->populate($formData);
        }
        } 
        elseif ($this->getRequest()->isPost() ) //Якщо відправлена форма з видаленням
            {
                if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == $this->yesButton) {
                $id = $this->getRequest()->getPost('id');
                $this->portfolioDBb->deleteData($id);
            }else { $this->_helper->redirector('edit');}
            $this->_helper->redirector('edit');
            }}
        
        else {//Виведення сторінки едіт
        $id = (int)$this->_getParam('id', 0);
        $del=(int)$this->_getParam('del', 0);
        $this->view->get_id=$id;
        if ($id > 0 && $del==0) {
        $formdata=$this->portfolioDBb->getDataID($id);
        if (isset($formdata)){$form->populate($formdata);}else{$this->view->get_id=0;};
       }elseif ($id > 0 && $del==1)
       {$this->view->get_del=1;
        $this->view->yesButton=$this->yesButton;
        $this->view->noButton=$this->noButton;
       $this->view->NameToDelete = $this->portfolioDBb->getDataID($id);
       }
        elseif ($id == 0) {
            $this->view->get_id = $id;
            }
        else { $this->view->get_id=0;
            echo "<span style='color: red;'>Error id: $id<br></span>";
        }
      }

    }

    public function deleteAction()
    {
        // action body
    }


}







